<?php

/**
 +------------------------------------------------------------------------------
 * 专家操作类
 +------------------------------------------------------------------------------
 * @author    yilon <yilon123456@gmail.com>
 +------------------------------------------------------------------------------
 */

class ArticleAction extends AdminAction {

    public function _initialize() {
//        $this->checkLogin();
        parent::_initialize();
    }

    /* 文章列表 */
    public function index() {
        $articleDao = D('Article');
        $cateDao = D('Category');
        $article_cateid = $_GET['article_cateid'];
        if($article_cateid) {
            $where = 'article_cateid ='.$article_cateid;
        }
        import("ORG.Page");
        $count = $articleDao->where($where)->count();
        $baseSelect = "article_id,article_title,article_cateid,article_top,article_ctime";//基本要顯示的字段
        $page = new Page($count, C('PAGE_LIMIT'));
        $list = $articleDao->field($baseSelect)->where($where)->order($articleDao->article_order)->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('article_cateid',$article_cateid);
        $this->assign('cateOption',$cateDao->getOptionByList());
        $this->assign('articleList', $list); // 赋值数据集
        $this->assign('page', $page->show()); // 赋值分页输出
        $this->assign($this->base_title,'文章列表');
        $this->display();
    }

    /* 发布修改文章 */

    public function editArticle() {
        $cateDao = D('Cate');
        $cateDao->getOptionByList();
        $article_id = $_GET['id'];
        if($article_id) {//修改文章
            $this->assign($this->base_title,'修改文章');
            $articleDao = D('Article');
            $res = $articleDao->find($article_id);
            $this->assign('res', $res);
        } else {
            $this->assign($this->base_title,'添加文章');
        }
        $this->assign('res', $res);
        $this->assign('cateOption',$cateDao->getOptionByList());
        $this->display();
    }

    function saveArticle() {
        $articleDao = D('Article');
        $article_id = $_POST['article_id'];
        if($article_id) {//修改
            $rs = $articleDao->articleSave($_POST,1);
        } else {//新增
            $rs = $articleDao->articleSave($_POST,0);
        }
        if($rs) {
            $this->success('保存成功！');
        } else {
            $this->error('保存失败！'.$articleDao->getError());
        }
    }

    function articleDel() {
        parent::delete('Article');
    }

    /*
     *
     */
    public function cates() {
        $cateModel = D('Cate');
        $list = $cateModel->selectByList();
        $this->assign('list',$list);
        $this->assign($this->base_title,'文章类型');
        $this->display();
    }

    /*
     * 添加、修改文章类别
     */
    public function editCate() {
        $cateDao = D('Cate');
        $cateDao->getOptionByList();
        $cate_id = $_GET['id'];
        if($cate_id) {//修改文章
            $this->assign($this->base_title,'修改文章分类');
            $cateDao = D('Cate');
            $res = $cateDao->find($cate_id);
        } else {
            $res = array('cate_parentid'=>$_GET['cate_parentid']);
            $this->assign($this->base_title,'添加文章分类');
        }
        $this->assign('res', $res);
        $this->assign('cateOption',$cateDao->getOptionByList());
        $this->display();
    }

    public function saveCate() {
        $cateDao = D('Cate');
        $cate_id = $_POST['cate_id'];
        if($cate_id) {//修改
            $rs = $cateDao->editCate($_POST,1);
        } else {//新增
            $rs = $cateDao->editCate($_POST,0);
        }
        if($rs) {
            $this->success('保存成功！');
        } else {
            $this->error('保存失败！'.$cateDao->getError());
        }
    }

    public function delCate() {
        $cateDao = D('Cate');
        $cate_id = $_GET['id'];
        $rs = $cateDao->delCate($cate_id);
        if($rs) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！'.$cateDao->getError());
        }
    }

}

?>